package cn.flaty.utils.web.file;

import org.apache.commons.lang.StringUtils;

import cn.flaty.utils.web.file.UploadInfo;

public class FileValidChecker {

	public final static int UNLIMIT = -1;
	
	public final static String  checkFileSize_MESSAGE= "文件大小不符！";
	public final static String  checkContentType_MESSAGE= "文件类型不符！";

	private long allowFileSize = UNLIMIT;

	private String allowTypes = null;

	private UploadInfo fileInfo;

	
	
	
	public long getAllowFileSize() {
		return allowFileSize;
	}


	public String getAllowTypes() {
		return allowTypes;
	}


	public UploadInfo getFileInfo() {
		return fileInfo;
	}


	public void setAllowFileSize(long allowFileSize) {
		this.allowFileSize = allowFileSize;
	}

	
	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}

	
	public void setFileInfo(UploadInfo fileInfo) {
		this.fileInfo = fileInfo;
	}

	/**
	 * 检查文件类型匹配
	 * 
	 * @author flatychen
	 * @date 2014-4-18
	 * @param mfile
	 * @return
	 */
	protected boolean checkContentType() {
		if (StringUtils.isBlank(allowTypes))
			return true;
		if (fileInfo.getFileContentType() != null) {
			String types[] = allowTypes.split(",");
			for (int i = 0; i < types.length; i++) {
				if (StringUtils.equalsIgnoreCase(fileInfo.getFileContentType(),
						types[i])) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 检查文件大小匹配
	 * 
	 * @author flatychen
	 * @date 2014-4-18
	 * @param mfile
	 * @return
	 */
	protected boolean checkFileSize() {
		if (!fileInfo.isEmpty()) {
			if (allowFileSize == FileValidChecker.UNLIMIT
					|| allowFileSize >= fileInfo.getFileSize()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}
